namespace TSL.DAL
{
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Data;
    using System.Data.Common;
    using System.Text;
    using TSL.Model;

    public class GetOpenCodes
    {
        public DataSet GetListFromView(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from V_GetOpenCode");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public DataSet GetListFromViewLast(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 * from V_GetOpenCodeLast");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by nowtime");
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public DataSet GetListGetHis(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 5 * from V_GetHisSSC");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by id desc ");
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public GetOpenCodeBuySSCID GetModelBySSCID(string ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_OpenCode_GetModelBySSCID");
            db.AddInParameter(dbCommand, "ssc_id", DbType.AnsiString, ID);
            GetOpenCodeBuySSCID model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = this.ReaderBindBuySSCID(dataReader);
                }
            }
            return model;
        }

        public GetOpenCode GetModel(string ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_GetOpenCode");
            db.AddInParameter(dbCommand, "ID", DbType.AnsiString, ID);
            GetOpenCode model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = this.ReaderBind(dataReader);
                }
            }
            return model;
        }

        public DataSet GetWinner(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from V_GetWinner");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("  order by convert(decimal,wincost) desc ");
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public DataSet GetWinnerList(string parmer, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            if (!string.IsNullOrEmpty(parmer))
            {
                strSql.Append(string.Format("select top 10 {0} from V_GetWinnerList", parmer));
            }
            else
            {
                strSql.Append("select top 10 * from V_GetWinnerList");
            }
            
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("  order by convert(decimal,wincost) desc ");
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public GetOpenCodeBuySSCID ReaderBindBuySSCID(IDataReader dataReader)
        {
            GetOpenCodeBuySSCID model = new GetOpenCodeBuySSCID();
            model.id = dataReader["id"].ToString();
            model.lottery_num = dataReader["lottery_num"].ToString();
            model.begintime = dataReader["begintime"].ToString();
            model.endtime1 = dataReader["endtime1"].ToString();
            model.endtime2 = dataReader["endtime2"].ToString();
            model.ssc_id = dataReader["ssc_id"].ToString();
            model.nowTime = int.Parse(dataReader["nowTime"].ToString());
            model.numinfo = dataReader["numinfo"].ToString();
            model.lastIssue = dataReader["lastIssue"].ToString();
            return model;
        }

        public GetOpenCode ReaderBind(IDataReader dataReader)
        {
            GetOpenCode model = new GetOpenCode();
            model.id = dataReader["id"].ToString();
            model.lottery_num = dataReader["lottery_num"].ToString();
            model.begintime = dataReader["begintime"].ToString();
            model.endtime1 = dataReader["endtime1"].ToString();
            model.endtime2 = dataReader["endtime2"].ToString();
            model.resttime = dataReader["resttime"].ToString();
            model.restday = dataReader["restday"].ToString();
            model.state = dataReader["state"].ToString();
            model.issale = dataReader["issale"].ToString();
            model.ssc_id = dataReader["ssc_id"].ToString();
            return model;
        }
    }
}

